package com.robot.command;

import iqq.im.bean.QQMsg;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

import com.robot.core.Context;
import com.robot.util.TextUtils;

public class AddActor extends CommandAcotr{
	public AddActor(Context context, QQMsg msg) {
		super(context, msg);
	}

	@Override
	public void execute() {
		String msgStr = msg.getFrom().getNickname()+"：";
		boolean validate = false;
		if("成语".equals(param1)&&Pattern.matches("^.{4}$", param2)){
			addIdiom(param2);
			validate = true;
		}
		if(validate){
			msgStr += param1+"添加成功。【"+param2+"】";
		}else{
			msgStr += "添加失败,正确格式【#ADD 类型 合法参数】。";
		}
		replyQQMsg(msgStr);
         
	}

	private void addIdiom(String idiom) {
		CYJLAction.getInstance(context).add(idiom);
		String userPath = System.getProperty("user.dir");
		File file = new File(new File(userPath),"source/idiom/自定义成语.txt");	
		List<String> list = TextUtils.readLine(file.getAbsolutePath(), "UTF-8");
		list.add(idiom);
		TextUtils.writeLine(list, file.getAbsolutePath(),"UTF-8");
	}

}
